layui.use(['table','layer','form','laydate'], function() {
	var table = layui.table;
	var layer = layui.layer;
	var form = layui.form;
	var laydate = layui.laydate;
	var $ = layui.jquery;
	
	//新增弹窗中的保存添加事件
		$("body").on("click","#saveBtn",function(){	//二次选择
			//获取form的全部数据
			var data = form.val("userForm");
			//console.log(data);
			//把数据推送道后台
			//ajax
			$.post("/user/register",data,function(msg){
				layer.closeAll();

				layer.msg(msg);
				console.log(msg)
				$("#searchBtn").click();
			});
		});
	
	
	//给新增按钮绑定事件
	$("#addBtn").on("click",function(){		
		//弹出窗口
		layer.open({
			type:1
			,area:['400px','500px']//窗口大小
			,title:"新增用户"
			,content:$("#addUserTemplate").html()
		});
		form.render();
		
		/*laydate.render({
			elem:'#birthday'//指定元素
		});*/
		
		//新增弹窗中的保存添加事件
		/*$("#saveBtn").on("click",function(){	
			//获取form的全部数据
			var data = form.val("userForm");
			//console.log(data);
			//把数据推送道后台
			//ajax
			$.post("/user/save",data,function(){
				layer.closeAll();
				$("#searchBtn").click();
			});
		});*/
		
	});
	
	
	
	//给查询按钮绑定事件
	$("#searchBtn").on("click",function(){
		table.render({
			elem: '#userTable'
			, url: '/user/list' //数据接口
			, page: true //开启分页
			, cols: [[ //表头
				{ field: 'id', title: 'ID', width: '10%', fixed: 'left' }
				, { field: 'username', title: '用户名', width: '20%' }
				, { field: 'sex', title: '性别', width: '20%'}
				, { field: 'name', title: '姓名', width: '20%'}
				, { field: 'role', title: '角色', width: '10%' }
				, { title: '操作', width: '20%' ,toolbar:"#operateTemplate"}
			]],
			limits:[5,10,15,20],
			limit:5,
			where:{"keyword":$("#searchUsername").val()}
		});
		
		table.on('tool(userTable1)',function(obj){
			
			var userid=obj.data.id;//获取当前操作行的id
			
			if(obj.event == "del" ){//点击删除按钮
				layer.confirm("你确定要删除该用户么？",function(){
					$.post("/user/del",{id:userid},function(){
						layer.closeAll();
						$("#searchBtn").click();
					});
				}/*,function(){}*/);
			}else if(obj.event == "edit"){//点击编辑按钮
				$.get("/user/"+userid,function(u){
					layer.open({
						type:1
						,area:['400px','500px']//窗口大小
						,title:"编辑用户"
						,content:$("#addUserTemplate").html()
					});
					form.val("userForm",u);//第一个获取值，第二个赋值给u
					
					/*$("#saveBtn").on("click",function(){	
						//获取form的全部数据
						var data = form.val("userForm");
						//console.log(data);
						//把数据推送道后台
						//ajax
						$.post("/user/save",data,function(){
							layer.closeAll();
							$("#searchBtn").click();
						});
					});*/
					
				});
			}else{
				$.get("/role/all",{userId:userid},function(roles){
						
						layer.open({
							type:1
							,title:'绑定角色'
							,area:['400px',"200px"]
							,content:$("#userRoleTemplate").html()
						});
						$.each(roles,function(index,value){
							$("#userRoleDiv").append("<input type='checkbox' "+(value.selected?'checked':'')+" value='"+value.id+"' title='"+value.name+"'>");
						});
						form.render();
						form.on('checkbox',function(data){
							//console.log(data);
							var roleId = data.value;
							//userid
							$.post("/userRole/operate",{userId:userid,roleId:roleId,type:data.elem.checked?'add':'cancel'},function(){
								
							});
						});
				});
				
			}
			
		});
		
	}).click();
	
});